/*
 * RawDataFileWriterHelper.java
 *
 * Created on 12 June 2007, 19:35
 *
 * Copyright (C)
 * This program is free software; you can redistribute it and/or modify it under 
 * the terms of the GNU General Public License as published by the Free Software 
 * Foundation; either version 2 of the License, or (at your option) any later 
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with 
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple 
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */
package com.grubby.databean.utils;

import com.grubby.utils.StringUtils;
import java.io.IOException;
import java.io.Writer;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * This class provides methods to help writing raw data to file tidily.
 *
 * @author A. HARMEL-LAW. E: andrew.harmel.law@gmail.com
 */
public class RawDataFileWriterHelper {

    /** Singleton */
    private RawDataFileWriterHelper() {
    }

    /**
     * Static helper method which writes a header comment for raw data files
     * @param headerContent array containing user defined lines for the header
     * comment.
     * @param aRandomSeed Random seed used to generate the dummy data.  If this 
     * @param sqlWriter Writer for the output
     * @param commentChar The character to be used to ensure this is comment and 
     * not data.  I.e. for SQL files, use '-'.
     * is null (i.e. no Random Seed was specified) then this is noted as opposed
     * to the value used
     * <P>You should always have at least a one-line description such as the
     * purpose of the dummy data.
     */
    public static void writeHeaderComment(String[] headerContent, 
            Long aRandomSeed, Writer sqlWriter, char commentChar) {
        
        // Calculate the width of the comment (for the surrounds)
        int commentWidth = 46;
        for (String elem : headerContent) {
            if (elem.length() > commentWidth) {
                commentWidth = elem.length();
            }
        }

        String sideBar = "" + commentChar + commentChar + commentChar;
        String sideBarTop = sideBar + commentChar;
        
        // Write the header comment
        try {

            sqlWriter.write(sideBarTop + StringUtils.paddingString("", commentWidth, commentChar, false) + sideBarTop + "\n");

            for (String elem : headerContent) {
                sqlWriter.write(sideBar + " ");
                sqlWriter.write(StringUtils.paddingString(elem, commentWidth, ' ', false));
                sqlWriter.write(" " + sideBar + "\n");
            }

            sqlWriter.write(sideBar + " ");
            sqlWriter.write(StringUtils.paddingString("Generated on: " + (new Date()).toString(), commentWidth, ' ', false));
            sqlWriter.write(" " + sideBar + "\n");

            sqlWriter.write(sideBar + " ");
            if (aRandomSeed == null) {
                sqlWriter.write(StringUtils.paddingString("Random Seed: NOT SPECIFIED", commentWidth, ' ', false));
            } else {
                sqlWriter.write(StringUtils.paddingString("Random Seed: " + aRandomSeed, commentWidth, ' ', false));
            }
            sqlWriter.write(" " + sideBar + "\n");

            sqlWriter.write(sideBarTop);
            sqlWriter.write(StringUtils.paddingString("", commentWidth, commentChar, false));
            sqlWriter.write(sideBarTop + "\n");
            
            sqlWriter.write("\n");

        } catch (IOException ex) {

            Logger.getLogger(RawDataFileWriterHelper.class.getName()).log(Level.SEVERE, null, ex);

        }
    }
}
